<template>
  <el-main>
    <el-table
      ref="multipleTable"
      :data="tableData3"
      tooltip-effect="dark"
      style="width: 100%"
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="username" label="用户名" width="120"></el-table-column>
      <el-table-column prop="mobile" label="电话" width="120"></el-table-column>
      <el-table-column prop="isDelete" label="状态" show-overflow-tooltip>
        <template slot-scope="scope">{{scope.row.isDelete | startUser}}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="small"
            :type="scope.row.isDelete ? 'danger':'success'"
            @click="changeUserState(scope.row)"
          >{{ scope.row.isDelete ? '禁用':'启用' }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="CurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      ></el-pagination>
    </div>
  </el-main>
</template>
<script>
import { queryUser, updateUser } from '@/api'
export default {
  created () {
    this.initList()
  },
  data () {
    return {
      // 当前页
      currentPage4: 1,
      total: 100,
      pagesize: 5,
      // 用户列表
      tableData3: [],
      multipleSelection: []
    }
  },
  filters: {
    startUser (val) {
      if (val === 1) {
        return '已启用'
      } else {
        return '未启用'
      }
    }
  },
  methods: {
    // 初始化页面
    initList () {
      queryUser({ page: this.currentPage4, pageSize: this.pagesize })
        .then(res => {
          console.log(res)
          this.total = res.data.total
          this.tableData3 = res.data.rows
        })
    },
    // 禁用按钮
    changeUserState (val) {
      console.log(val)
      updateUser({ id: val.id, isDelete: val.isDelete ? 0 : 1 })
        .then(res => {
          console.log(res)
          this.initList()
        })
    },
    toggleSelection (rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleDelete (index, row) {
      console.log(index, row)
    },
    setCurrent (row) {
      this.$refs.singleTable.setCurrentRow(row)
    },
    handleCurrentChange (val) {
      this.currentRow = val
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.initList()
    },
    CurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.currentPage4 = val
      this.initList()
    }
  }
}
</script>
<style lang="scss" scoped>
.el-table::before {
  height: 0;
}
</style>
